<template>
	<div>
		<AplidForm ref="formRef" v-bind="BaseFormConfig" v-model="formModel" @submit="onSubmit" @reset="toBack">
			<!-- 下发对象 -->
			<template v-slot:bscTaskEvaluatorInfoListForm>
				<div>
					<div class="flex align-center">
						<el-button type="primary" size="small" @click="handleOpenSelect(1)">刷选器</el-button>
						已选择({{ evaluatorNum }})
					</div>
				</div>
			</template>
			<!-- 居家下发对象 -->
			<template v-slot:homePractitionerListForm>
				<div>
					<div class="flex align-center">
						<el-button type="primary" size="small" @click="handleOpenSelect(5)">刷选器</el-button>
						已选择({{ homePractitionerNum }})
					</div>
				</div>
			</template>
			<!-- 评估对象-(居家)老人 -->
			<template v-slot:serviceObjectHomeListForm>
				<div>
					<div class="flex align-center">
						<el-button type="primary" size="small" @click="handleOpenSelect(4)">刷选器</el-button>
						已选择({{ serviceObjectHomeNum }})
					</div>
				</div>
			</template>
			<!-- 评估对象-(机构)老人 -->
			<template v-slot:dpServiceObjectListForm>
				<div>
					<div class="flex align-center">
						<el-button type="primary" size="small" @click="handleOpenSelect(2)">刷选器</el-button>
						已选择({{ serviceObjectNum }})
					</div>
				</div>
			</template>
			<!-- 评估对象-机构 -->
			<template v-slot:dpServiceOrgBaseListForm>
				<div>
					<div class="flex align-center">
						<el-button type="primary" size="small" @click="handleOpenSelect(3)">刷选器</el-button>
						已选择({{ serviceOrgNum }})
					</div>
				</div>
			</template>
		</AplidForm>

		<el-drawer
			v-model="drawer"
			:close-on-click-modal="false"
			:title="listType == 1 ? '选择下发对象' : '选择评估对象'"
			size="60%"
			:lock-scroll="false"
			:modal="false"
			append-to-body
		>
			<practitioners
				ref="evaluator"
				v-if="listType == 1"
				:key="key"
				:listType="listType"
				@selection-change="onSelectionChange"
				:selectedData="selectedData"
			>
			</practitioners>
			<serviceObj
				ref="serviceObject"
				v-if="listType == 2"
				:listType="listType"
				:key="key"
				@selection-change="onSelectionChange"
				:selectedData="selectedData"
			>
			</serviceObj>
			<serviceOrgan
				ref="serviceOrg"
				v-if="listType == 3"
				:key="key"
				:listType="listType"
				@selection-change="onSelectionChange"
				:selectedData="selectedData"
			>
			</serviceOrgan>
			<!-- 居家老人 -->
			<homeServiceObj 
				ref="serviceOrg"
				v-if="listType == 4"
				:key="key"
				:listType="listType"
				@selection-change="onSelectionChange"
				:selectedData="selectedData">
			</homeServiceObj>
			<!-- 居家从业人员 -->
			<homePractitioners 
				ref="serviceOrg"
				v-if="listType == 5"
				:key="key"
				:listType="listType"
				@selection-change="onSelectionChange"
				:selectedData="selectedData">
			</homePractitioners>
			<template #footer>
				<div class="f-between w-100">
					<p>请勾选</p>
					<el-button type="primary" @click="drawer = !drawer">返回</el-button>
				</div>
			</template>
		</el-drawer>
	</div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
// import Filters from '../Filters/index.vue'
import apiMod from '@/apiMod/index'
import { BaseFormConfig } from '../config'
import practitioners from '../Filters/practitioners.vue'
import serviceObj from '../Filters/serveiceObj.vue'
import serviceOrgan from '../Filters/serviceOrgan.vue'
import homeServiceObj from '@/views/oneStopOffice/issuanceManagement/Filters/homeServiceObj/index.vue'
import homePractitioners from '../Filters/practitioners.vue'
import {getStore} from '@/utils/store'


type Props = {
	config?: any
	nowRowData?: any
	stepNum?: any
	formData?: any
	formType?: any
}
const props = withDefaults(defineProps<Props>(), {
	config: {},
	nowRowData: {},
	stepNum: 1,
	formData: {},
	formType: 'add',
})
const emit = defineEmits(['refresh', 'gatherParams', 'back'])

// 选中的表格数据
let selectedData = ref([])
let userInfo = getStore('userInfo')
const toBack = () => {
	emit('refresh')
}


// 需要三个对象来存储数据
let bscTaskEvaluatorInfoList = ref([])
let dpServiceObjectList = ref([])
let dpServiceOrgBaseList = ref([])
let serviceObjectHomeList = ref([])
let homePractitionerList = ref([])

let evaluatorNum = ref(0)
let serviceObjectNum = ref(0)
let serviceObjectHomeNum = ref(0)
let homePractitionerNum = ref(0)
let serviceOrgNum = ref(0)

// 获取到抽屉数据
let key = ref(1)
let formModel: any = ref({})
formModel.value = { ...props.nowRowData, ...props.formData }
let drawer = ref(false)
// 通过接口获取勾选的数据
const getApiSelect = async (editData, api) => {
	if (editData) {
		let list = editData.split(',')
		let params = {
			idList: list,
			pageParaPO: { size: 100000, current: 1 },
			isWithAuth:0
		}
		let res = await apiMod[api](params)
		selectedData.value = res.data.records
		key.value = Math.random()
		console.log('编辑2',selectedData.value)
		return res.data.records
	} else {
		return []
	}
}
// 初始化列表和数据
if (props.formType == 'edit') {
	console.log('prop11', props.nowRowData)

	if (props.nowRowData.evaluatorIds) {
		let list = props.nowRowData.evaluatorIds.split(',')
		evaluatorNum.value = list.length
		homePractitionerNum.value = list.length
		if (list.length > 0) {
			formModel.value.bscTaskEvaluatorInfoList = true
		} else {
			formModel.value.bscTaskEvaluatorInfoList = undefined
		}
	}
	if (props.nowRowData.serviceObjectIds) {
		let list = props.nowRowData.serviceObjectIds.split(',')
		serviceObjectNum.value = list.length
		serviceObjectHomeNum.value = list.length

		if (list.length > 0) {
			formModel.value.dpServiceObjectList = true
		} else {
			formModel.value.dpServiceObjectList = undefined
		}
	}
	if (props.nowRowData.serviceOrgIds) {
		let list = props.nowRowData.serviceOrgIds.split(',')
		serviceOrgNum.value = list.length
		if (list.length > 0) {
			formModel.value.dpServiceOrgBaseList = true
		} else {
			formModel.value.dpServiceOrgBaseList = undefined
		}
	}
	if (props.nowRowData.startTime && props.nowRowData.endTime) {
		props.nowRowData.taskTimeRange = [props.nowRowData.startTime, props.nowRowData.endTime]
	}
}

const formRef = ref()
// 赋值前先处理时间范围

const onSubmit = async (form, done) => {
	// 处理时间段
	if (form.taskTimeRange) {
		form.startTime = form.taskTimeRange[0]
		form.endTime = form.taskTimeRange[1]
		form.taskTimeRange = undefined
	}
	form.evaluatorIds = undefined
	form.serviceObjectIds = undefined
	form.serviceOrgIds = undefined
	form.evaluatorJson = undefined
	form.evaluatorIdCards = undefined
	
	if (props.formType == 'add') {
		if(userInfo.classifyId==5&&form.taskType == 1){
			
				form.bscTaskEvaluatorInfoList = (homePractitionerList.value.length>0?homePractitionerList.value:undefined)
				form.dpServiceObjectList = (dpServiceObjectList.value.length > 0 ? 	dpServiceObjectList.value : undefined) 
			
		}else{
			if(form.taskType == 1){
			form.bscTaskEvaluatorInfoList =(bscTaskEvaluatorInfoList.value.length>0?bscTaskEvaluatorInfoList.value:undefined)
			form.dpServiceObjectList = (dpServiceObjectList.value.length > 0 ? 	dpServiceObjectList.value : undefined) 
			}
			if(form.taskType == 2){
				form.bscTaskEvaluatorInfoList = (bscTaskEvaluatorInfoList.value.length>0?bscTaskEvaluatorInfoList.value:undefined)
				form.dpServiceOrgBaseList =  (dpServiceOrgBaseList.value.length > 0 ? 	dpServiceOrgBaseList.value : undefined) 
			}
			if(form.taskType == 3){
				form.bscTaskEvaluatorInfoList = (homePractitionerList.value.length>0?homePractitionerList.value:undefined)
				form.dpServiceObjectList =  (serviceObjectHomeList.value.length > 0 ? 	serviceObjectHomeList.value : undefined) 
			}
		}
		apiMod.view_bscTaskInfo_add(form).then((res) => {
			emit('refresh')
			done()
		})
	} else {
		// 通过id列表拿到数据
		if(userInfo.classifyId==5&&form.taskType == 1){
			
				form.bscTaskEvaluatorInfoList = await getApiSelect(formModel.value.evaluatorIds, 'view_dpFoundationPractitioners_page')
				form.dpServiceObjectList = await getApiSelect(formModel.value.serviceObjectIds, 'view_dpServiceObject_page')
			
		}else{
			if (form.taskType == 1) {
			form.dpServiceObjectList = await getApiSelect(formModel.value.serviceObjectIds, 'view_dpServiceObject_page')
			form.bscTaskEvaluatorInfoList = await getApiSelect(formModel.value.evaluatorIds, 'view_dpFoundationPractitioners_page')
			}
			if (form.taskType == 2) {
				form.dpServiceOrgBaseList = await getApiSelect(formModel.value.serviceOrgIds, 'view_dpServiceOrgBase_page')
				form.bscTaskEvaluatorInfoList = await getApiSelect(formModel.value.evaluatorIds, 'view_dpFoundationPractitioners_page')
			}
			if(form.taskType == 3){
				form.dpServiceObjectList = await getApiSelect(formModel.value.serviceObjectIds, 'view_dpServiceObjectHome_page')
				form.bscTaskEvaluatorInfoList = await getApiSelect(formModel.value.evaluatorIds, 'view_dpFoundationPractitioners_page')
			}
		}
		
		
		apiMod.view_bscTaskInfo_edit(form).then((res) => {
			emit('refresh')
			done()
		})
	}
}

const onSave = () => {
	formRef.value!.onSubmit()
}

// 打开刷选器
let listType: any = ref('')
const handleOpenSelect = (type) => {
	// 得到打开类型
	selectedData.value = []
	listType.value = type
	// 拿到勾选的数据
	if (props.formType == 'add') {
		if (type == 1) {
			selectedData.value = bscTaskEvaluatorInfoList.value
		} else if (type == 2) {
			selectedData.value = dpServiceObjectList.value
		} else if (type == 3) {
			selectedData.value = dpServiceOrgBaseList.value
		} else if(type==4){
			// 居家老人
			selectedData.value = serviceObjectHomeList.value
		}else if(type==5){
			// 居家从业人员
			// selectedData.value = bscTaskEvaluatorInfoList.value
			selectedData.value = homePractitionerList.value
		}
	} else {
		console.log('编辑',type)
		if (type == 1) {
			getApiSelect(formModel.value.evaluatorIds, 'view_dpFoundationPractitioners_page')
		} else if (type == 2) {
			getApiSelect(formModel.value.serviceObjectIds, 'view_dpServiceObject_page')
		} else if (type == 3) {
			getApiSelect(formModel.value.serviceOrgIds, 'view_dpServiceOrgBase_page')
		} else if(type==4){
			getApiSelect(formModel.value.serviceObjectIds, 'view_dpServiceObjectHome_page')
		}else if(type==5){
			getApiSelect(formModel.value.evaluatorIds, 'view_dpFoundationPractitioners_page')
		}
	}
	drawer.value = true
}

const onSelectionChange = (arr) => {
	// 立即查看时回显
	if (listType.value == 1) {
		bscTaskEvaluatorInfoList.value = arr
		evaluatorNum.value = arr.length
		let list = arr.map((el) => {
			return el.id
		})
		if (Array.isArray(arr) && arr.length > 0) {
			formModel.value.bscTaskEvaluatorInfoList = true
		} else {
			formModel.value.bscTaskEvaluatorInfoList = undefined
		}
		formModel.value.evaluatorIds = list.join(',')
	} else if (listType.value == 2) {
		dpServiceObjectList.value = arr
		serviceObjectNum.value = arr.length
		let list = arr.map((el) => {
			return el.id
		})
		if (Array.isArray(arr) && arr.length > 0) {
			formModel.value.dpServiceObjectList = true
		} else {
			formModel.value.dpServiceObjectList = undefined
		}
		formModel.value.serviceObjectIds = list.join(',')
	} else if (listType.value == 3) {
		dpServiceOrgBaseList.value = arr
		serviceOrgNum.value = arr.length
		let list = arr.map((el) => {
			return el.id
		})
		if (Array.isArray(arr) && arr.length > 0) {
			formModel.value.dpServiceOrgBaseList = true
		} else {
			formModel.value.dpServiceOrgBaseList = undefined
		}
		formModel.value.serviceOrgIds = list.join(',')
	}else if (listType.value == 4) {
		serviceObjectHomeList.value = arr
		serviceObjectHomeNum.value = arr.length
		let list = arr.map((el) => {
			return el.id
		})
		if (Array.isArray(arr) && arr.length > 0) {
			formModel.value.serviceObjectHomeList = true
		} else {
			formModel.value.serviceObjectHomeList = undefined
		}
		formModel.value.serviceObjectIds = list.join(',')
	}else if(listType.value == 5){
		homePractitionerList.value = arr
		homePractitionerNum.value = arr.length
		let list = arr.map((el) => {
			return el.id
		})
		if (Array.isArray(arr) && arr.length > 0) {
			formModel.value.homePractitionerList = true
		} else {
			formModel.value.homePractitionerList = undefined
		}
		formModel.value.evaluatorIds = list.join(',')
	}
	selectedData.value = arr
}



defineExpose({
	onSave,
})
</script>

<style scoped></style>
